import 'dart:async';
import 'dart:developer';
import 'dart:io';

import 'package:boss/common/common.dart';
import 'package:boss/provider/login_user_provider.dart';
import 'package:boss/provider/version_provider.dart';
import 'package:boss/ui/widget/app_version_up.dart';
import 'package:flutter_baidutts/flutter_baidutts.dart';
import 'package:hi_common/hi_common.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:provider/provider.dart';

import 'jpush_constant.dart';

class JPushUtil {
  factory JPushUtil() => _getInstance();

  static JPushUtil _getInstance() {
    if (_instance == null) {
      _instance = JPushUtil._internal();
    }

    return _instance;
  }

  static JPushUtil _instance;

  static JPushUtil get instance => _getInstance();

  JPushUtil._internal() {
    jPush = JPush();
  }

  JPush jPush;

  init() async {
    log("jPush初始化", name: "极光推送");

    jPush.setup(
      appKey: Constant.jPushAppKey,
      channel: 'developer-default',
      debug: true,
      production: false,
    );

    jPush.applyPushAuthority(NotificationSettingsIOS(sound: true, alert: true, badge: false));

    jPush.addEventHandler(
        // 接收通知回调方法。
        onReceiveNotification: onReceiveNotification,
        // 点击通知回调方法。
        onOpenNotification: onOpenNotification,
        // 接收自定义消息回调方法。
        onReceiveMessage: onReceiveMessage,
        onReceiveNotificationAuthorization: (message) async {
          log('onReceiveNotificationAuthorization', name: '极光推送');
        });

    Constant.jPushId = await jPush.getRegistrationID();

    log("jPushID:${Constant.jPushId}", name: "极光推送");
  }

  Future<dynamic> onReceiveMessage(Map<String, dynamic> message) async {
    try {
      log('onReceiveMessage jpush message: $message', name: '极光推送');

      final data = Platform.isAndroid ? message['extras']['cn.jpush.android.EXTRA'] : message['extras'];

      final msg = Platform.isAndroid ? message['message'] : message['content'];

      switch (data['type']) {
        case JPushConstant.VERSION_UP:
          appVersionUp(data);
          break;
        case JPushConstant.RECOVER_TABLE_QR_CODE_AMOUNT:
          final user = Provider.of<LoginUserProvider>(GlobalPageContext.instance.context, listen: false).loginUser;
          if (user == null) return;

          FlutterBaidutts.instance.speak(msg);
          break;
        default:
          break;
      }
    } catch (e) {
      print('推送发生未知错误：$e');
    }
  }

  Future<dynamic> onReceiveNotification(Map<String, dynamic> message) async {
    log('onReceiveNotification', name: '极光推送');
  }

  Future<dynamic> onOpenNotification(Map<String, dynamic> message) async {}

  //升级
  appVersionUp(Map<String, dynamic> data) {
    var downloadUrl = data['styleParam']['appVersion']['packages'];
    var version = data['styleParam']['appVersion']['version'];
    var content = data['styleParam']['appVersion']['versionExternal'];
    var forceUpdate = data['styleParam']['appVersion']['updateScope'] == 0;
    if (version == Constant.appVersion) {
    } else if (data['styleParam']['appVersion']['redDotWarn'] == 0) {
      //如果是红点升级
      var versionProvider = Provider.of<VersionProvider>(
        GlobalPageContext.instance.context,
        listen: false,
      );
      versionProvider.setRedDot(true);
    } else {
      showAppVersionUpModal(
        GlobalPageContext.instance.context,
        version: version,
        content: content,
        downloadUrl: downloadUrl,
        forceUpdate: forceUpdate,
      );
    }
  }
}
